package com.palm.hubo2o.ui.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.palm.hubo2o.R;
import com.palm.hubo2o.event.BetEvent;
import com.palm.hubo2o.model.Bet;
import com.palm.hubo2o.util.EnumCode;
import com.palm.palmutils.MathUtils;
import com.palm.palmutils.widget.EasyRecyclerView;

import java.util.Locale;

import de.greenrobot.event.EventBus;

/**
 * Created by dest1 on 2015/10/15.
 * 组3单式
 */
public class D3Z3DFragment extends MostLotteryFragment {
    private int lastRepeat = -1;
    private int lastSingle = -1;
    private final Bet bet = new Bet(EnumCode.gameId.D3);

    @Override
    public void onCreateView2(View view, Bundle savedInstanceState) {
        super.onCreateView2(view, savedInstanceState);
        setupBalls(null, 0, 10, 10);
        bet.setPlayType(EnumCode.PlayType.D3_Z3DS);
    }

    @Override
    protected void onSetMarkText(View parent, int groupIndex) {
        TextView title = (TextView) parent.findViewById(R.id.title_mark);
        TextView tip = (TextView) parent.findViewById(R.id.tip_mark);
        if (groupIndex == 0) {
            title.setText("重号");
            tip.setVisibility(View.VISIBLE);
            tip.setText("选择1个重号和1个单号");
        } else {
            title.setText("单号");
            tip.setVisibility(View.VISIBLE);
        }
    }

    @Override
    protected int onSetGroupColor(int groupIndex) {
        return red;
    }

    @Override
    public boolean onRandomBalls() {
        onClearChoices();
        int[] cc = MathUtils.randomIn(10, 2);
        mGrid.setItemChecked(getAdapter().transToRawPosition(cc[0], 0), true);
        mGrid.setItemChecked(getAdapter().transToRawPosition(cc[1], 1), true);

        return true;
    }

    @Override
    protected void onClearChoices() {
        super.onClearChoices();
        lastRepeat = -1;
        lastSingle = -1;
    }

    @Override
    protected boolean onBallChecked(EasyRecyclerView parent, boolean checked, int position, String ballText, int index) {
        if (index == 0) {//重号
            if (checked) {
                if (lastRepeat != -1)
                    parent.setItemChecked(getAdapter().transToRawPosition(lastRepeat, 0), false);
                if (parent.isItemChecked(getAdapter().transToRawPosition(position, 1)))//互斥
                    parent.setItemChecked(getAdapter().transToRawPosition(position, 1), false);
                lastRepeat = position;
            } else {
                if (position == lastRepeat)
                    lastRepeat = -1;
            }

        } else {//单号
            if (checked) {
                if (lastSingle != -1)
                    parent.setItemChecked(getAdapter().transToRawPosition(lastSingle, 1), false);
                if (parent.isItemChecked(getAdapter().transToRawPosition(position, 0)))//互斥
                    parent.setItemChecked(getAdapter().transToRawPosition(position, 0), false);
                lastSingle = position;
            } else {
                if (position == lastSingle)
                    lastSingle = -1;
            }
        }

        if (lastRepeat >= 0 && lastSingle >= 0) {
            bet.getReds().clear();
            bet.getReds().add(String.format(Locale.getDefault(), "%d%d%d", lastRepeat, lastRepeat, lastSingle));
            bet.setWagerCount(1);
            EventBus.getDefault().post(new BetEvent(bet));
        } else {
            EventBus.getDefault().post(new BetEvent(null));
        }
        return true;
    }
}
